#!/usr/bin/env python3
import os
import json
import argparse
import traceback
import VmManage


def usage():
    pname = os.path.basename(__file__)
    print(pname + " --ip <vcenter ip> --user <user> --password <password> [--vm_name <vm_name>|--vm_ip <vm_ip>] ")
    exit(1)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", default="", help="vcenter管理IP")
    parser.add_argument("--port", default="443", help="vcenter管理端口")
    parser.add_argument("--user", default="", help="vcenter console login user")
    parser.add_argument("--password", default="", help="vcenter console login user password")
    parser.add_argument("--vm_name", default="", help="clone new virtual machine name")
    parser.add_argument("--vm_ip", default="", help="clone new virtual machine ip")
    parser.add_argument("--verbose", default="0", help="verbose output")
    args = parser.parse_args()

    ip = args.ip
    user = args.user
    password = args.password
    port = args.port
    vm_name = args.vm_name
    vm_ip = args.vm_ip
    verbose = int(args.verbose)

    node = os.getenv("AUTOEXEC_NODE")
    if node != None and node != "":
        node = json.loads(node)

    if (ip == None or ip == "" or user == None or user == "" or password == None or password == "") and (node == None or node == ""):
        usage()

    if (vm_name is None or vm_name == "") and (vm_ip is None or vm_ip == ""):
        usage()

    if (ip == None or ip == "" or user == None or user == "" or password == "" or password == None) and node != None:
        ip = node["host"]
        port = 443
        if "protocolPort" in node:
            port = node["protocolPort"]
        else:
            port = node["port"]
        user = node["username"]
        password = node["password"]

    param = {}
    param["vm_name"] = vm_name
    param["vm_ip"] = vm_ip
    param["verbose"] = verbose

    vm = VmManage.VmManage(ip, user, password, port)
    ret = vm.destroy_vm(param)
    vm.close()
    exit(ret)
